﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using ListForms;

namespace ListsForm
{
    public partial class FrmEditCompany : FrmBaseEditForm
    {
        public FrmEditCompany(Lists.Company company)
        {
            InitializeComponent();
            if (company == null)
            {
                _EditState = EditState.CreatingNew;
                bindingSource.AddNew();
                this.Text = "New Company";
            }
            else
            {
                _EditState = EditState.Modifying;
                bindingSource.Add(DB.Companies.FirstOrDefault(p => p.CompanyID == company.CompanyID));
                this.Text = "Edit Company";
            }
        }

        protected override void CheckFullInfo()
        {
            Lists.Company company = (Lists.Company)bindingSource.Current;
            if (string.IsNullOrEmpty(company.CompanyNo))
                throw new Exception("Company ID not allow null");
            if (string.IsNullOrEmpty(company.CompanyName))
                throw new Exception("Company name not allow null");
            if (string.IsNullOrEmpty(company.CompanyTel))
            {
                throw new Exception("Company tel not allow null");
            }
            Regex regexnumber = new Regex(@"[\d]");
            if (!regexnumber.IsMatch(company.CompanyTel))
            {
                throw new Exception("Company tel number only");
            }
        }

        protected override void SaveAndClose()
        {
            Lists.Company company = (Lists.Company)bindingSource.Current;
            if (_EditState == EditState.CreatingNew)
                DB.Companies.InsertOnSubmit(company);
            DB.SubmitChanges();
            this.Close();
        }
    }
}
